﻿Public Class Passform

    Dim frxSender As FractalEncrypt
    Dim frxReceiver As FractalEncrypt
    Dim CurSeq As Integer

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub
    Dim txtnumbertxt As String
    Private Sub Passform_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        txtnumber = False
        If Form1.ComboBox1.SelectedItem = "AES (Rijndael)" Then
            lbl_pwd3.Enabled = True
            tb_pwd3.Enabled = True
        End If
        If Form1.ComboBox1.SelectedItem = "Fractal encryption" Then
            txtnumber = True
            txtnumbertxt = tb_pwd1.Text
        End If
        tb_pwd1.Focus()
    End Sub

    Private Sub lbl_close_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl_close.MouseDown
        lbl_close.ForeColor = Color.DarkGray
    End Sub

    Private Sub lbl_close_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl_close.MouseUp
        lbl_close.ForeColor = Color.WhiteSmoke
        tmr_fade.Enabled = True
        tmr_fade.Start()
    End Sub

    Private Sub tmr_fade_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_fade.Tick
        Dim opacFact As Double = 0.05
        Me.Opacity = Me.Opacity - opacFact

        If (Me.Opacity = 0) Then
            Me.Close()
            tmr_fade.Stop()
        End If
    End Sub


    Private Sub lbl_close_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl_close.MouseHover
        lbl_close.ForeColor = Color.Gainsboro
    End Sub

    Private Sub lbl_close_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl_close.MouseLeave
        lbl_close.ForeColor = Color.Black
    End Sub

    Private Sub Passform_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        a = Control.MousePosition.X - Me.Location.X
        b = Control.MousePosition.Y - Me.Location.Y

    End Sub
    Dim newPoint As New System.Drawing.Point()
    Dim a As Integer
    Dim b As Integer

    Private Sub Passform_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None Then

            If e.Button = MouseButtons.Left Then
                newPoint = Control.MousePosition
                newPoint.X = newPoint.X - (a)
                newPoint.Y = newPoint.Y - (b)
                Me.Location = newPoint

            End If

        End If
    End Sub

    Private Sub tmr_appear_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_appear.Tick
        Dim opacFact As Double = 0.05
        Do While tmr_fade.Enabled = True
            Exit Sub
        Loop
        Me.Opacity = Me.Opacity + opacFact
        If (Me.Opacity = 1) Then
            tmr_appear.Stop()
        End If
    End Sub

    Private Sub btn_signin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_signin.Click
        If My.Settings.Encrypt = True Then
            If Form1.ComboBox1.SelectedItem = "DES" Then
                Form1.Timer1.Start()
                tmr_fade.Start()
                Encryption.Encoding1(tb_pwd1.Text, Form1.RichTextBox1.Text)
            ElseIf Form1.ComboBox1.SelectedItem = "AES (Rijndael)" Then
                Dim length As String = tb_pwd3.Text
                For l As Integer = Len(tb_pwd3.Text) To 15
                    length = length + "Q"
                Next
                If Len(tb_pwd3.Text) > 16 Then
                    MessageBox.Show("Password three must be shorter than 17 characters")
                    Exit Sub
                End If
                Form1.Timer1.Start()
                My.Settings.text = RijndaelSimple.Encrypt(Form1.RichTextBox1.Text, tb_pwd1.Text, tb_pwd2.Text, "SHA1", 14, length, 256)
                tmr_fade.Start()
            ElseIf Form1.ComboBox1.SelectedItem = "Fractal encryption" Then
                If tb_pwd1.Text = "" Then
                    MessageBox.Show("Enter a password")
                    Exit Sub
                End If
                If frxSender Is Nothing Then InitiateHandshake()
                CurSeq = CurSeq + 1
                Dim txt As String = frxSender.Encrypt(Form1.RichTextBox1.Text, CurSeq)
                Form1.Timer1.Start()
                My.Settings.text = Replace(txt, Chr(0), "#") 'controls cannot handle nul (ascii 0).
                tmr_fade.Start()
            End If
        Else
            Try
                If Form1.ComboBox1.SelectedItem = "DES" Then
                    Form1.Timer1.Start()
                    Encryption.Decode1(tb_pwd1.Text, Form1.RichTextBox1.Text)
                    tmr_fade.Start()
                ElseIf Form1.ComboBox1.SelectedItem = "AES (Rijndael)" Then
                    If Len(tb_pwd3.Text) > 16 Then
                        MessageBox.Show("Password three must be shorter than 17 characters")
                        Exit Sub
                    End If
                    Dim length As String = tb_pwd3.Text
                    For l As Integer = Len(tb_pwd3.Text) To 15
                        length = length + "Q"
                    Next
                    Form1.Timer1.Start()
                    My.Settings.text = RijndaelSimple.Decrypt(Form1.RichTextBox1.Text, tb_pwd1.Text, tb_pwd2.Text, "SHA1", 14, length, 256)
                    tmr_fade.Start()
                ElseIf Form1.ComboBox1.SelectedItem = "Fractal encryption" Then
                    If tb_pwd1.Text = "" Then
                        MessageBox.Show("Enter a password")
                        Exit Sub
                    End If
                    If frxSender Is Nothing Then InitiateHandshake()
                    CurSeq = CurSeq + 1
                    Form1.Timer1.Start()
                    My.Settings.text = frxReceiver.Decrypt(Form1.RichTextBox1.Text)
                    tmr_fade.Start()
                End If
            Catch
                MessageBox.Show("Incorrect Password")
            End Try
        End If

    End Sub
       Public txtnumber As Boolean = False
    Private Sub Label1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
        Label1.ForeColor = Color.Gainsboro
    End Sub




    Private Sub Label1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
        Label1.ForeColor = Color.LightGray
        About_box.Show()
        If Label1.Text = "Tips for a secure password" Then
            About_box.lbl_title.Text = "Secure Password"
            About_box.Label1.Text = "Try to make passwords at lease eight characters long." + Environment.NewLine + "Also try to make it have capital letters, numbers and if " + Environment.NewLine + "possible, symbols."
        ElseIf Label1.Text = "Numbers only!" Then
            About_box.lbl_title.Text = "Numbers only!"
            About_box.Label1.Text = "Use only numbers in your passwords when using the Fractal" + Environment.NewLine + "encryption algorithm!"
        Else
            About_box.lbl_title.Text = "AES (Rijndael)"
            About_box.Label1.Text = "Three passwords are needed for the rijndael algorithm to make it " + Environment.NewLine + "lots more secure."

        End If
        About_box.tmr_appear.Start()
    End Sub

    Private Sub Passform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tb_pwd1.Focus()
    End Sub

    Private Sub Label1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
        Label1.ForeColor = Color.Silver
    End Sub

    Private Sub Label1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
        Label1.ForeColor = Color.Black
    End Sub
   

    Private Sub InitiateHandshake()
        'Create the new encryption objects & simultate key transmission
        Dim kr As Double, ki As Double, si As Double, sr As Double, SeqStart As Integer

        'First extract the master key set
        kr = tb_pwd1.Text
        ki = tb_pwd1.Text
        sr = tb_pwd1.Text
        si = tb_pwd1.Text
        SeqStart = tb_pwd1.Text
        'NOTE: these 5 values togehter form the master key for encrypting
        '   and decrypting communications between the sender and receiver.

   

        'Make the Senders Encryption object
        frxSender = New FractalEncrypt(kr, ki, sr, si, SeqStart)
        CurSeq = 0

        'Simulate transmission of the master keys to the receiver...
        '------
        '
        '   (Now, the master keys are transmitted securely to the receiver...)
        '
        '------
        'NOTE: secure key transmission is usually done with 1-way keys (like RSA)
        '   as it cannot be done securely with 2-way keys alone.

        'Make the Receivers Encryption object from the Master Keys that were just sent to it.
        frxReceiver = New FractalEncrypt(kr, ki, sr, si, SeqStart)

        ' Now both sides have encryption objects with mathcing keys and are ready to
        'use encrypted communications ...
    End Sub

    Private Sub tb_pwd1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb_pwd1.TextChanged
        If txtnumber = True Then
            Dim l As Integer = Len(tb_pwd1.Text)
            If tb_pwd1.Text = "" Then
            Else
                If Not IsNumeric(tb_pwd1.Text) Then
                    tb_pwd1.Text = txtnumbertxt
                    tb_pwd1.SelectionStart = l
                End If
                txtnumbertxt = tb_pwd1.Text
                tb_pwd1.SelectionStart = l
            End If
        End If
    End Sub

  
End Class
